package com.zp.crm.service.UserService.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zp.crm.dao.RoidDao.RoidDao;
import com.zp.crm.dao.UserDao.UserDao;
import com.zp.crm.dto.User.UserDto;
import com.zp.crm.entity.Roid.RoidEntity;
import com.zp.crm.entity.User.UserEntity;
import com.zp.crm.service.MenuService.MenuService;
import com.zp.crm.service.UserService.UserService;
import com.zp.crm.util.MD5.MD5CC;
import com.zp.crm.util.targetLog.Log;
@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Resource
	public UserDao userDao;
	@Resource
	public RoidDao roidDao;
	/*登录*/
	@Override
	@Log(name="登录")
	public Map<String, Object> LoginSelectByName(String username, String password) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		
		MD5CC md5=new MD5CC();
		
		boolean c=username.indexOf("-")==-1&&username.indexOf("#")==-1&&username.indexOf("@")==-1;
		
		boolean a=true;
		
		if(!c){
			a=false;
		}
		if(a){
			UserEntity user=userDao.selectByNameLogin(username);
			if(user != null){
				if(user.getUserStuts().equals("锁定中") || user.getUserStuts().equals("已删除") || user.getUserSDCS()==1){
					if(user.getUserSDCS()==1){
						map.put("success", false);
						map.put("message", "您的账号已被锁定！请联系管理员！");
					}
					if(user.getUserStuts().equals("锁定中") && user.getUserSDCS()==0){
						map.put("success", false);
						map.put("message", "您的账号处于锁定中！请明天再试！");
					}
					if(user.getUserStuts().equals("已删除")){
						map.put("success", false);
						map.put("message", "您的账号已被删除！请联系管理员！");
					}
				}else{
					String pw=user.getUserPwd();
					pw=md5.string2MD5(pw);
					if(pw.equals(password)){
						if(user.getUserStuts().equals("正常")){
							map.put("success", true);
							map.put("message", "index.html");
							map.put("LoginUid", user.getCode());
							map.put("LoginUname",user.getUserTrueName());
						}
					}else{
						map.put("success", false);
						map.put("message", "密码错误！请从新输入！");
					}
				}
			}else{
				map.put("success",false);
				map.put("message","此账号不存在！");
			}
		}else{
			map.put("success", false);
			map.put("message", "您的输入账号包含限制符号（@#-）！");
		}
		return map;
	}
	/*信息列表*/
	@Log(name="用户信息列表查询业务")
	@Override
	public Map<String, Object> SelectByList(int page, int size) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int PageIndex=(page-1)*size;
		
		int count=userDao.selectByCount();
		
		List<UserEntity> list=userDao.selectList(PageIndex,size);
		
		String a=new UserDto().CreateListHtml(count,list,page,0);
		
		map.put("message", a);
		
		return map;
	}
	@Log(name="用户信息修改操作业务")
	@Override
	public Map<String, Object> UserStutsUpdate(UserEntity entity) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.UserStutsUpdate(entity);
		
		if(r>0){
			map.put("success", true);
			map.put("message", "操作成功！");
		}else{
			map.put("success", false);
			map.put("message", "操作失败！请刷新后从试！");
		}

		return map;
	}
	@Log(name="用户信息条件查询信息业务层")
	@Override
	public Map<String, Object> SelectCreatePage(String username, String name, String stuts, int page, int size) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(username != null || name != null || stuts != null){
			int PageIndex=(page-1)*size;
			int count=userDao.selectCreateCount(username,name,stuts);
			List<UserEntity> list=userDao.selectCreateList(username,name,stuts,PageIndex,size);
			String a=new UserDto().CreateListHtml(count,list,page,1);
			map.put("message", a);
		}else{
			map=SelectByList(page,size);
		}
		return map;
	}
	@Log(name="用户权限修改操作")
	@Override
	public Map<String, Object> UserRoidUpdate(String code, String obj) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.UserRoidUpdate(code,obj);
		
		if(r>0){
			map.put("success", true);
			map.put("message", "修改成功！");
		}else{
			map.put("success", false);
			map.put("message", "修改失败！");
		}
		
		return map;
	}
	@Log(name="用户信息检测账号真实姓名是否存在")
	@Override
	public Map<String, Object> UserNameTrueNameSelect(String name, String tname) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.UserNameTrueNameSelect(name,tname);
		
		if(r>0){
			map.put("success", false);
		}else{
			map.put("success", true);
		}
		return map;
	}
	@Log(name="用户信息注册")
	@Override
	public Map<String, Object> insert(UserEntity entity) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.insert(entity);
		
		if(r>0){
			map.put("success", true);
			map.put("message", "注册用户成功！");
		}else{
			map.put("success", false);
			map.put("message", "注册用户失败！");
		}
		return map;
	}
	@Log(name="用户登录Ip和时间修改 ")
	@Override
	public Map<String, Object> LoginIpAndTime(String code,String ip, Date date) {
		Map<String,Object> map=new HashMap<String,Object>();

		int r=userDao.LoginIpAndTime(code,ip,date);
		
		if(r>0){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		
		return map;
	}
	@Log(name="用户信息根据编号查询权限并拼接成字符串形式")
	@Override
	public Map<String, Object> selectById(String code) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		UserEntity entity=userDao.selectBykeyId(code);
		
		if(entity.getUserRoid()!=null||!entity.getUserRoid().equals("")){
			String[] obj=entity.getUserRoid().split(",");
			
			List<Integer> list=new ArrayList<>();
			
			if(obj != null){
				for(int i=0;i<obj.length;i++){
					list.add(Integer.parseInt(obj[i]));
				}
			}

			List<RoidEntity> roidlist=roidDao.selectBykeyUserRoidList(list);
			
			String a="";
			
			for(int t=0;t<roidlist.size();t++){
				RoidEntity roide=roidlist.get(t);
				if(a.equals("")){
					a=roide.getName();
				}else{
					a=a+","+roide.getName();
				}
			}
			
			entity.setRoidStr(a);
			
			map.put("success", true);
			
			map.put("message", entity);
		}else{
			map.put("success", false);
			
			map.put("message", "系统错误！");
		}
		
		return map;
	}
	@Log(name="用户信息密码验证 ")
	@Override
	public Map<String, Object> SelectUserNamePwd(String name, String pwd) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.SelectUserNamePwd(name,pwd);
		
		if(r>0){
			

			map.put("success", true);
			
			map.put("message", "正确");
			
		}else{

			map.put("success", false);
			
			map.put("message", "错误！");
		}
		
		return map;
	}
	@Log(name="用户信息密码修改")
	@Override
	public Map<String, Object> EditUserPwd(String code, String pwd) {
		Map<String,Object> map=new HashMap<String,Object>();
		
		int r=userDao.EditPwd(code,pwd);
		
		if(r>0){
			

			map.put("success", true);
			
			map.put("message", "修改成功");
			
		}else{

			map.put("success", false);
			
			map.put("message", "修改失败！");
		}
		return map;
	}
	@Log(name="用户信息修改照片 ")
	@Override
	public Map<String, Object> EditUserImg(String code, String fileName) {
		Map<String,Object> map=new HashMap<String,Object>();
		int r=userDao.editImg(code,fileName);
		if(r>0){
			map.put("success", true);
			map.put("message", "修改成功");
		}else{
			map.put("success", false);
			map.put("message", "图片上传失败！");
		}
		return map;
	}
	@Log(name="专属通道用户查询")
	@Override
	public Map<String, Object> UserListPrivate() {
		Map<String, Object> map=new HashMap<>();
		
		List<UserEntity> list=userDao.selectByPrivateList();
		
		StringBuffer buffer=new StringBuffer();
		
		int i=1;
		
		int b=0;
		
		for (UserEntity u : list) {
			b++;
			if(b<19){
				buffer.append(u.getUserTrueName()+","+u.getUserName()+","+u.getCode()+","+b+","+i+",");
			}else{
				b=1;
				i++;
				buffer.append(u.getUserTrueName()+","+u.getUserName()+","+u.getCode()+","+b+","+i+",");			}	
		}
		int t=list.size();	
		if(t>0){
			map.put("success", true);
			map.put("message",buffer);
		}else{
			map.put("success", false);
			map.put("message", "暂无数据!");
		}
		
		return map;
	}
	

	@Log(name="测试批量添加用户")
	@Override
	public Map<String, Object> UserTestListAdd(List<UserEntity> list) {
		
		Map<String, Object> map=new HashMap<>();
		
		int tb=0;
		
		int bc=0;
		
		for (UserEntity u : list) {
			Map<String, Object> tmap=UserNameTrueNameSelect(u.getUserName(),u.getUserTrueName());
			if((boolean) tmap.get("success")){
				Map<String, Object> amap=insert(u);
				if((boolean) amap.get("success")){
					tb++;
				}
			}else{
				bc++;
			}
		}
		
		int c=tb+bc;
		
		map.put("message", "重复姓名用户为："+bc+"添加成功条数为"+tb+"总条数为："+c);
			
		return map;
	}	
}
